/**
 * 
 */
package com.platform.common.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

/**
 * <pre>
 * 功能：时间处理工具类
 * </pre>
 * 
 * @author wei.gao
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	
	public static final long SECOND = 1000L;
	public static final long MINUTE = 60000L;
	public static final long HOUR = 3600000L;
	public static final long DAY = 86400000L;
	public static final long WEEK = 604800000L;
	public static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
	public static final String PATTERN_YMDHMSS = "yyyyMMddHHmmssSSS";
	public static final String PATTERN_YMDHMS = "yyyyMMddHHmmss";
	//日期格式
	public static final String PATTERN_YYYYMMDD = "yyyyMMdd";
	public static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
	public static final String PATTERN_YYYY_MM_DD_HH_mm = "yyyy-MM-dd HH:mm";
	public static final String PATTERN_MM_DD_HH_mm = "MM-dd HH:mm";
	public static final String PATTERN_YYYY_MM_DD_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
	
	private static Map<String, DateFormat> formatMap = new HashMap<String, DateFormat>();
	
	/**
	 * <pre>
	 * 功能：获得昨天的时间
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getYestoday() {
		return truncate(getDateAgo(1), Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：获得前days天的这个时候
	 * </pre>
	 * 
	 * @param days
	 * @return
	 */
	public static Date getDateAgo(int days) {
		return getDateAgo(new Date(), days);
	}
	
	/**
	 * <pre>
	 * 功能：获得某时间之前days天的时间
	 * </pre>
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date getDateAgo(Date date, int days) {
		return addDays(date, -days);
	}
	
	/**
	 * <pre>
	 * 功能：获得days天后的时间
	 * </pre>
	 * 
	 * @param days
	 * @return
	 */
	public static Date getDateAfter(int days) {
		return getDateAgo(new Date(), -days);
	}
	
	/**
	 * <pre>
	 * 功能：获得某个时间之后days天的时间
	 * </pre>
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date getDateAfter(Date date, int days) {
		return getDateAgo(date, -days);
	}
	
	/**
	 * <pre>
	 * 功能：获得本周第一天
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getMondayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0) {
			day_of_week = 7;
		}
		c.add(Calendar.DATE, -day_of_week + 1);
		return c.getTime();
	}
	
	/**
	 * <pre>
	 * 功能：获得本周最后一天
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getLastDayOfThisWeek() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_WEEK, 7);
		c.add(Calendar.DATE, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：获得本月第一天
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getFirstDayOfThisMonth() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_MONTH, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：获得本月最后一天
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getLastDayOfThisMonth() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.add(Calendar.DATE, -1);
		c.add(Calendar.MONTH, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：获得上周的最后一天
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getLastDayOfLastWeek() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_WEEK, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：获得上周的第一天
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getFirstDayOfLastWeek() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_WEEK, 1);
		c.add(Calendar.DATE, -6);
		return truncate(c.getTime(), Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：获得上个月的最后一天
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getLastDayOfLastMonth() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.add(Calendar.DATE, -1);
		return truncate(c.getTime(), Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：获得上个月第一天
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getFirstDayOfLastMonth() {
		Calendar c = new GregorianCalendar();
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：获得某个日期的零值
	 * </pre>
	 * 
	 * @return
	 */
	public static Date getDateZero() {
		Date date = new Date();
		return truncate(date, Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：获得某个日期的零值
	 * </pre>
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDateZero(Date date) {
		date = (date == null ? new Date() : date);
		return truncate(date, Calendar.DATE);
	}
	
	/**
	 * <pre>
	 * 功能：格式化日期
	 * </pre>
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public synchronized static String format(Date date, String pattern) {
		DateFormat format = formatMap.get(pattern);
		if (format == null) {
			format = new SimpleDateFormat(pattern, Locale.CHINESE);
			
			formatMap.put(pattern, format);
		}
		return format.format(date);
	}
	
	/**
	 * <pre>
	 * 功能：格式化日期
	 * </pre>
	 * 
	 * @param dateString
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static String format(String dateString, String pattern) throws ParseException {
		Date date = parse(dateString, pattern);
		dateString = format(date, pattern);
		return dateString;
	}
	
	/**
	 * <pre>
	 * 功能：解析时间字符串
	 * </pre>
	 * 
	 * @param dateString
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String dateString, String format) throws ParseException {
		return parse(dateString, format, Locale.CHINESE, TimeZone.getDefault());
	}
	
	/**
	 * <pre>
	 * 功能：解析时间字符串
	 * </pre>
	 * 
	 * @param dateString
	 * @param pattern
	 * @param locale
	 * @param timeZone
	 * @return
	 * @throws ParseException
	 */
	public synchronized static Date parse(String dateString, String pattern, Locale locale, TimeZone timeZone) throws ParseException {
		DateFormat format = formatMap.get(pattern);
		if (format == null) {
			format = new SimpleDateFormat(pattern, Locale.CHINESE);
			
			formatMap.put(pattern, format);
		}
		format.setTimeZone(timeZone);
		Date date = format.parse(dateString);
		return date;
	}
	
	/**
	 * <pre>
	 * 功能：获得当前时间的格式
	 * </pre>
	 * 
	 * @param pattern
	 * @return
	 */
	public static String getCurrentTime(String pattern) {
		return format(new Date(), pattern);
	}
	
	/**
	 * <pre>
	 * 功能：获得两个时间的间隔天数
	 * </pre>
	 * 
	 * @param sDate
	 * @param eDate
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(Date sDate, Date eDate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sDate = sdf.parse(sdf.format(sDate));
		eDate = sdf.parse(sdf.format(eDate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(sDate);
		long startDate = cal.getTimeInMillis();
		cal.setTime(eDate);
		long endDate = cal.getTimeInMillis();
		long between_days = (startDate - endDate) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}
	
	/**
	 * <pre>
	 * 功能：获得两个时间的间隔小时
	 * </pre>
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getIntervalHours(Date begin, Date end) {
		if (begin == null || end == null) {
			return 0;
		}
		long between = (end.getTime() - begin.getTime()) / 1000;
		return (int) between / 3600;
	}
	
	/**
	 * <pre>
	 * 功能：获得两个时间的间隔分钟
	 * </pre>
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getIntervalMinites(Date begin, Date end) {
		if (begin == null || end == null) {
			return 0;
		}
		long between = (end.getTime() - begin.getTime()) / 1000;
		return (int) between / 60;
	}
	
	/**
	 * <pre>
	 * 功能：获得两个时间的间隔秒
	 * </pre>
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getIntervalSecond(Date begin, Date end) {
		
		if (begin == null || end == null)
			return 0;
		long between = (end.getTime() - begin.getTime()) / 1000;
		return (int) between;
	}
	
	/**
	 * <pre>
	 * 功能：获得SQL时间
	 * </pre>
	 * 
	 * @param date
	 * @return
	 */
	public static java.sql.Date getSqlDate(Date date) {
		if (date == null) {
			return null;
		}
		return new java.sql.Date(date.getTime());
	}
	
	/**
	 * <pre>
	 * 功能：获得SQL时间戳
	 * </pre>
	 * 
	 * @param date
	 * @return
	 */
	public static Timestamp getSqlTimestamp(Date date) {
		if (date == null) {
			return null;
		}
		return new java.sql.Timestamp(date.getTime());
	}
	
	/**
	 * <pre>
	 * 功能：实现了时间加号和减号 
	 * 条块: y: year m: month d: date h:hour f: minute s:second
	 * </pre>
	 * 
	 * @param date
	 * @param type
	 * @param timeInterval
	 * @return
	 */
	public static Date computeDate(Date date, char type, int timeInterval) {
		if (date == null) {
			date = new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		
		int Time_year = cal.get(Calendar.YEAR);
		int Time_month = cal.get(Calendar.MONTH);
		int Time_day = cal.get(Calendar.DAY_OF_MONTH);
		int Time_hour = cal.get(Calendar.HOUR_OF_DAY);
		int Time_minute = cal.get(Calendar.MINUTE);
		int Time_second = cal.get(Calendar.SECOND);
		
		switch (type) {
		case 'y':
			Time_year = Time_year + timeInterval;
			cal.set(Calendar.YEAR, Time_year);
			break;
		case 'm':
			Time_month = Time_month + timeInterval;
			cal.set(Calendar.MONTH, Time_month);
			break;
		case 'd':
			Time_day = Time_day + timeInterval;
			cal.set(Calendar.DAY_OF_MONTH, Time_day);
			break;
		case 'h':
			Time_hour = Time_hour + timeInterval;
			cal.set(Calendar.HOUR_OF_DAY, Time_hour);
			break;
		case 'f':
			Time_minute = Time_minute + timeInterval;
			cal.set(Calendar.MINUTE, Time_minute);
			break;
		case 's':
			Time_second = Time_second + timeInterval;
			cal.set(Calendar.SECOND, Time_second);
			break;
		default:
			break;
		}
		date = cal.getTime();
		return date;
	}
}